package cn.ronghuanet.base.exception;

import org.apache.commons.lang3.StringUtils;

import java.util.Collection;
import java.util.Collections;
import java.util.Objects;

/**
 * 断言工具类
 */
public class RonghuaAssert {

    // 断言字符串为空
    /**
     * 当字符串不为空的时候,要抛出异常
     * lang3包里面,做字符串非空校验时,isBlank和isEmpty他们的区别是什么??
     * "        " isBlank()这是个空字符串
     * "        " isEmpty() 认识这不是一个空字符串
     * @param str
     * @param message
     */
    public static void isBlank(String str,String message){
        if(StringUtils.isNotBlank(str)){
            throw new BusinessException(message);
        }
    }

    // 断言字符串不为空,当字符串为空时就要抛异常
    public static void isNotBlank(String str,String message){
        if(StringUtils.isBlank(str)){
            throw new BusinessException(message);
        }
    }

    // 断言对象为空
    public static void isNull(Object obj,String message){
        if(Objects.nonNull(obj)){
            throw new BusinessException(message);
        }
    }

    // 断言对象不为空
    public static void isNotNull(Object obj,String message){
        if(Objects.isNull(obj)){
            throw new BusinessException(message);
        }
    }

    // 断言集合为空,当集合不为空时就要抛异常
    public static void isNull(Collection collection, String message){
        if(collection != null && collection.size() > 0){
            throw new BusinessException(message);
        }
    }

    // 断言集合不为空,当集合为空时要抛异常
    public static void isNotNull(Collection collection, String message){
        if(collection == null || collection.size() == 0){
            throw new BusinessException(message);
        }
    }

    // 断言字符串是否相等,当两个字符串不相等时就要抛异常
    public static void isEq(String str1,String str2,String message){
        if(!str1.equals(str2)){
            throw new BusinessException(message);
        }
    }


    //=================================上面是new BusinessException(字符串)===============================

    public static void isBlank(String str,ResponseCode code){
        if(StringUtils.isNotBlank(str)){
            throw new BusinessException(code);
        }
    }

    // 断言字符串不为空,当字符串为空时就要抛异常
    public static void isNotBlank(String str,ResponseCode code){
        if(StringUtils.isBlank(str)){
            throw new BusinessException(code);
        }
    }

    // 断言对象为空
    public static void isNull(Object obj,ResponseCode code){
        if(Objects.nonNull(obj)){
            throw new BusinessException(code);
        }
    }

    // 断言对象不为空
    public static void isNotNull(Object obj,ResponseCode code){
        if(Objects.isNull(obj)){
            throw new BusinessException(code);
        }
    }

    // 断言集合为空,当集合不为空时就要抛异常
    public static void isNull(Collection collection,ResponseCode code){
        if(collection != null && collection.size() > 0){
            throw new BusinessException(code);
        }
    }

    // 断言集合不为空,当集合为空时要抛异常
    public static void isNotNull(Collection collection,ResponseCode code){
        if(collection == null || collection.size() == 0){
            throw new BusinessException(code);
        }
    }

    // 断言字符串是否相等,当两个字符串不相等时就要抛异常
    public static void isEq(String str1,String str2,ResponseCode code){
        if(!str1.equals(str2)){
            throw new BusinessException(code);
        }
    }
}
